/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BasePermissionAdminDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BasePermissionAdminDao : BaseDao
    {
        public BasePermissionAdminDao()
        {
            base.CurrentTableName = BasePermissionTable.TableName;
        }

        public BasePermissionAdminDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BasePermissionAdminDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BasePermissionAdminDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BasePermissionEntity permissionEntity, out string statusCode)
        {
            string str = string.Empty;
            if (this.Exists(BasePermissionTable.FieldCode, permissionEntity.Code))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return str;
            }
            str = this.AddEntity(permissionEntity);
            statusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        public string AddByDetail(string code, string fullName, out string statusCode)
        {
            BasePermissionEntity permissionEntity = new BasePermissionEntity
            {
                Code = code,
                FullName = fullName
            };
            return this.Add(permissionEntity, out statusCode);
        }

        public string AddEntity(BasePermissionEntity permissionEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BasePermissionTable.TableName);
            permissionEntity.SortCode = sequence;
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BasePermissionTable.TableName);
            if (string.IsNullOrEmpty(permissionEntity.ID))
            {
                permissionEntity.ID = sequence;
            }
            sqlBuilder.SetValue(BasePermissionTable.FieldID, permissionEntity.ID);
            this.SetEntity(sqlBuilder, permissionEntity);
            sqlBuilder.SetValue(BasePermissionTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BasePermissionTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return permissionEntity.ID;
        }

        public int BatchSave(DataTable dataTable)
        {
            int num = 0;
            BasePermissionEntity permissionEntity = new BasePermissionEntity();
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    string id = row[BasePermissionTable.FieldID, DataRowVersion.Original].ToString();
                    if ((id.Length > 0) && row[BasePermissionTable.FieldAllowDelete, DataRowVersion.Original].ToString().Equals("1"))
                    {
                        num += this.DeleteEntity(id);
                    }
                }
                if ((row.RowState == DataRowState.Modified) && (row[BasePermissionTable.FieldID, DataRowVersion.Original].ToString().Length > 0))
                {
                    permissionEntity.GetFrom(row);
                    if (permissionEntity.AllowEdit)
                    {
                        num += this.UpdateEntity(permissionEntity);
                    }
                    else
                    {
                        num += this.SetProperty(BasePermissionTable.FieldID, base.ID, BasePermissionTable.FieldSortCode, base.SortCode);
                    }
                }
                if (row.RowState == DataRowState.Added)
                {
                    permissionEntity.GetFrom(row);
                    num += (this.AddEntity(permissionEntity).Length > 0) ? 1 : 0;
                }
                if (row.RowState != DataRowState.Unchanged)
                {
                    DataRowState rowState = row.RowState;
                }
            }
            return num;
        }

        public int Delete(string id)
        {
            return this.DeleteEntity(id);
        }

        public DataTable GetByCode(string code)
        {
            return this.GetDT(BasePermissionTable.FieldCode, code);
        }

        public DataTable GetByCode(string code, BasePermissionEntity permissionEntity)
        {
            DataTable dT = this.GetDT(BasePermissionTable.FieldCode, code);
            permissionEntity.GetFrom(dT);
            return dT;
        }

        public DataTable GetDTByUser(string userID, string permissionID)
        {
            string sqlQuery = string.Empty;
            sqlQuery = " SELECT *   FROM ECP_Permission  WHERE ID IN (  SELECT ECP_ResourcePermissionScope.TargetID    FROM ECP_ResourcePermissionScope   WHERE (ECP_ResourcePermissionScope.ResourceCategory = 'User')         AND (ECP_ResourcePermissionScope.ResourceID = '" + userID + "')         AND (ECP_ResourcePermissionScope.TargetCategory = 'Permission')         AND (ECP_ResourcePermissionScope.PermissionID = '" + permissionID + "')         AND (ECP_ResourcePermissionScope.Enabled = 1)  UNION  SELECT ECP_ResourcePermissionScope.TargetID    FROM ECP_ResourcePermissionScope   WHERE (ECP_ResourcePermissionScope.ResourceCategory  = 'Role')         AND (ECP_ResourcePermissionScope.TargetCategory  = 'Permission')         AND (ECP_ResourcePermissionScope.PermissionID = '" + permissionID + "')         AND (ECP_ResourcePermissionScope.Enabled = 1)         AND (ECP_ResourcePermissionScope.ResourceID IN (              SELECT ECP_UserRole.RoleID                FROM ECP_UserRole               WHERE (ECP_UserRole.UserID  = '" + userID + "')                   AND (ECP_UserRole.Enabled = 1))))";
            DataTable dataTable = new DataTable(BasePermissionTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public BasePermissionEntity GetEntity(string id)
        {
            return new BasePermissionEntity(this.Get(id));
        }

        public int MoveTo(string id, string parentID)
        {
            return this.SetProperty(id, BaseOrganizeTable.FieldParentID, parentID);
        }

        private void SetEntity(SQLBuilder sqlBuilder, BasePermissionEntity permissionEntity)
        {
            sqlBuilder.SetValue(BasePermissionTable.FieldParentID, permissionEntity.ParentID);
            sqlBuilder.SetValue(BasePermissionTable.FieldCode, permissionEntity.Code);
            sqlBuilder.SetValue(BasePermissionTable.FieldFullName, permissionEntity.FullName);
            sqlBuilder.SetValue(BasePermissionTable.FieldSortCode, permissionEntity.SortCode);
            sqlBuilder.SetValue(BasePermissionTable.FieldDescription, permissionEntity.Description);
            sqlBuilder.SetValue(BasePermissionTable.FieldIsScope, permissionEntity.IsScope ? 1 : 0);
            sqlBuilder.SetValue(BasePermissionTable.FieldEnabled, permissionEntity.Enabled ? 1 : 0);
            sqlBuilder.SetValue(BasePermissionTable.FieldAllowEdit, permissionEntity.AllowEdit ? 1 : 0);
            sqlBuilder.SetValue(BasePermissionTable.FieldAllowDelete, permissionEntity.AllowDelete ? 1 : 0);
        }

        public int Update(BasePermissionEntity permissionEntity, out string statusCode)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, BasePermissionTable.TableName, permissionEntity.ID, permissionEntity.ModifyUserID, permissionEntity.ModifyDate))
            {
                statusCode = StatusCode.ErrorChanged.ToString();
                return num;
            }
            if (this.Exists(BasePermissionTable.FieldCode, permissionEntity.Code, permissionEntity.ID))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return num;
            }
            num = this.UpdateEntity(permissionEntity);
            if (num == 1)
            {
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        public int UpdateEntity(BasePermissionEntity permissionEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BasePermissionTable.TableName);
            this.SetEntity(sqlBuilder, permissionEntity);
            sqlBuilder.SetValue(BasePermissionTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BasePermissionTable.FieldModifyDate);
            sqlBuilder.SetWhere(BasePermissionTable.FieldID, permissionEntity.ID);
            return sqlBuilder.EndUpdate();
        }
    }
}